package net.chrisrichardson.ec2deployer.applications

import net.chrisrichardson.ec2deployer.cluster.ClusterSpec
import net.chrisrichardson.ec2deployer.core.EC2Server
import net.chrisrichardson.ec2deployer.cluster.DatabaseTier
import net.chrisrichardson.ec2deployer.cluster.TerracottaTier
import net.chrisrichardson.ec2deployer.cluster.WebServerTier

class SpringSourceApplicationPlatformServer extends AbstractApplicationServer {

    def platformHome = "/var/lib/springsource-ap-1.0.0.RC1"
    
    public SpringSourceApplicationPlatformServer(ClusterSpec clusterSpec, EC2Server host, DatabaseTier databaseTier, TerracottaTier terracottaTier, WebServerTier webServerTier) {
		super(clusterSpec, host, databaseTier, terracottaTier, webServerTier);
    }

    public void startTomcat() {
		assert host.isRunning()
		databaseTier.grantAccessToClient(this)

        exec "cat > .sapenv", """
export JAVA_OPTS='${makeCatalinaOpts()}'
export JAVA_HOME=/usr/java/default
        """

       stopTomcat()

		clusterSpec.webApps.each{it.updateTomcat(this, clusterSpec.bucketPrefix)}

		writeFile fileName: "$platformHome/config/servletContainer.config",
                templateName: "/templates/springSourceApplicationPlatform/servletContainer.config",
                templateArgs: [jvmRoute: jvmRoute]

        terracottaTier.prepareClient host

        exec "(. .sapenv ; cd $platformHome; nohup bin/startup.sh)  < /dev/null > /dev/null 2>&1 &"
	}

	public void stopTomcat() {
        exec ". .sapenv; cd $platformHome; bin/shutdown.sh"
    }

    String getDeploymentDirectory() {
        return "$platformHome/pickup"
    }

    boolean supportsExplodedWars() {
        return false
    }

}